package localizer.main;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;


public class CheckedArrayAdapter extends ArrayAdapter<String> {
    
    private LayoutInflater inflater;
    private Context context;
    public CheckedArrayAdapter( Context context, List<String> planetList ) {
      super( context, R.layout.simplerow, R.id.rowTextView, planetList );
      // Cache the LayoutInflate to avoid asking for a new one each time.
      this.context=context;
      inflater = LayoutInflater.from(context) ;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      // Planet to display
      String planet = (String) this.getItem( position ); 

      // The child views in each row.
      final CheckBox checkBox ; 
      TextView textView ; 
      
      // Create a new row view
      if ( convertView == null ) {
        convertView = inflater.inflate(R.layout.simplerow, null);
        
        // Find the child views.
        textView = (TextView) convertView.findViewById( R.id.rowTextView );
        checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
        

        // If CheckBox is toggled, update the planet it is tagged with.
        checkBox.setOnClickListener( new View.OnClickListener() {
          public void onClick(View v) {
        	  if(position==0){
        		  
        		  Config.getInstance(context).setEnableCheckRingByMsg(context, checkBox.isChecked());
        		  if(checkBox.isChecked()){
						Toast.makeText(context, "Ring By SMS is Enable",Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(context, "Ring By SMS is Disabled",Toast.LENGTH_LONG).show();
					}
        	  }else{
        		  Config.getInstance(context).setEnableCheckLocByMsg(context, checkBox.isChecked());
        		  if(checkBox.isChecked()){
						Toast.makeText(context, "Localization By SMS is Enable",Toast.LENGTH_LONG).show();
					}else{
						Toast.makeText(context, "Localization By SMS is Disabled",Toast.LENGTH_LONG).show();
					}
        	  }
          }
        });        
      }
      // Reuse existing row view
      else {
        // Because we use a ViewHolder, we avoid having to call findViewById().
        checkBox =  (CheckBox) convertView.findViewById( R.id.CheckBox01);
        textView = (TextView) convertView.findViewById( R.id.rowTextView);
      }

      // Display planet data
      if(position==0){
    	  checkBox.setChecked(Config.getInstance(context).isEnableCheckRingByMsg(context));
      }else{
    	  checkBox.setChecked(Config.getInstance(context).isEnableCheckLocByMsg(context));
      }
      textView.setText(planet );      
      
      return convertView;
    }
    
  }